﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class DayDAO : CoreAbstractDAO<Day>
    {
        private AdapterAbstractDAO<Day, DaySchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<Day, DayUniversity> wrappedAbstractDAOUniversity;
        private DayDAOSchool wrappedDAOSchool;
        private DayDAOUniversity wrappedDAOUniversity;

        public DayDAO(DayDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool =
                new AdapterAbstractDAO<Day, DaySchool>((AbstractDAO<DaySchool>)wrappedDAOSchool);
        }

        public DayDAO(DayDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<Day, DayUniversity>((AbstractDAO<DayUniversity>)wrappedDAOUniversity);
        }

        public override void update(Day entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.update(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped DayDAOSchool nor DayDAOUniversity was initialized");
            }
        }

        public override void save(Day entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped DayDAOSchool nor DayDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(Day entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped DayDAOSchool nor DayDAOUniversity was initialized");
            }
        }

        public override void delete(Day entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped DayDAOSchool nor DayDAOUniversity was initialized");
            }
        }

        public override Day getById(int id)
        {
            Day entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped DayDAOSchool nor DayDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<Day> getAll()
        {
            IList<Day> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped DayDAOSchool nor DayDAOUniversity was initialized");
            }

            return records;
        }
    }
}
